package cn.tedu.boot33.controller;

import cn.tedu.boot33.mapper.ItemMapper;
import cn.tedu.boot33.vo.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ItemController {
    @Autowired(required = false)
    ItemMapper mapper;

    @RequestMapping("/insert")
    public String insert(Item item){
        mapper.insert(item);
        return "添加成功<a href='/index.html'>返回首页</a>";
    }

    @RequestMapping("/select")
    public String select(){
        List<Item> list = mapper.select();
        String html = "<table border=1>";
        html += "<caption>商品列表</caption>";
        html += "<tr><th>id</th><th>名称</th><th>库存</th><th>价格</th><th>商品介绍</th><th>操作</th></tr>";
        for (Item item: list){
            html += "<tr>";
            html += "<td>"+item.getId()+"</td>";
            html += "<td>"+item.getName()+"</td>";
            html += "<td>"+item.getNum()+"</td>";
            html += "<td>"+item.getPrice()+"</td>";
            html += "<td>"+item.getIntro()+"</td>";
            html += "<td><a href='/delete?id="+item.getId()+"'>删除</a></td>";
            html += "</tr>";
        }
        html+="</table>";
        return html;
    }

    @RequestMapping("/delete")
    public String delete(int id){
        mapper.delete(id);
        return "删除成功<a href='/select'>返回商品列表</a>";
    }

    @RequestMapping("/update")
    public String update(Item item){
        mapper.update(item);
        return "修改成功<a href='/select'>返回商品列表</a>";
    }



}
